package com.guigu.systemservice.controller;


import com.guigu.commonutils.JwtUtils;
import com.guigu.commonutils.R;
import com.guigu.systemservice.entity.SysAccount;
import com.guigu.systemservice.service.SysAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author Jane
 * @since 2024-09-06
 */
@CrossOrigin
@Api(description = "SysAccount请求控制类")
@RestController
@RequestMapping("/systemservice/sys-account")
public class SysAccountController {
    @Autowired
    private SysAccountService sysAccountService;

    @ApiOperation("根据用户名和密码验证身份")
    @PostMapping("/login")
    public R login(@RequestBody SysAccount sysAccount) {
        String token = sysAccountService.queryByNameAndPass(sysAccount.getLoginName(),
                sysAccount.getPassword());
        return R.ok().data("token", token);
    }

    @ApiOperation("获取认证成功的用户信息")
    @GetMapping("/info")
    public R info(HttpServletRequest request) {
        String loginName = JwtUtils.getMemberIdByJwtToken(request);
        return R.ok().data("loginName", loginName);
    }

    @ApiOperation("查询所有SysAccount数据")
    @GetMapping("/list")
    public R list() {
        List<SysAccount> accountList = sysAccountService.list(null);
        return R.ok().data("accountList", accountList);
    }


    @ApiOperation("根据编号查询SysAccount数据")
    @GetMapping("/{accountId}")
    public R byId(
            @ApiParam("查询主键id")
            @PathVariable("accountId") String accountId) {
        SysAccount sysAccount = sysAccountService.getById(accountId);
        return R.ok().data("sysAccount", sysAccount);
    }
}

